package com.yonyou.chuan.impl;

import com.yonyou.chuan.model.vo.YhtFlowVO;
import com.yonyou.chuan.model.vo.YhtUserVO;
import com.yonyou.chuan.service.YhtPubService;
import nc.bs.dao.BaseDAO;
import nc.message.vo.MessageVO;
import nc.vo.bd.psn.PsndocVO;
import nc.vo.pf.flowinstance.FlowInstanceVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.workflownote.WorkflownoteVO;
import nc.vo.sm.UserVO;

import java.util.Collection;

public class YhtPubServiceImpl implements YhtPubService {

    private static BaseDAO basedao = null;

    public synchronized BaseDAO getBaseDao() {
        if (basedao == null) {
            basedao = new BaseDAO();
        }
        return basedao;
    }

    @Override
    public YhtUserVO queryByUserId(String userId) throws BusinessException {
        YhtUserVO yhtUserVO = new YhtUserVO();
        yhtUserVO.setUserId(userId);
        //TODO 查询SM_USER
        UserVO userVO = (UserVO)getBaseDao().retrieveByPK(UserVO.class, userId);
        yhtUserVO.setUserCode(userVO.getUser_code());
        yhtUserVO.setUserName(userVO.getUser_name());
        yhtUserVO.setStaffId(userVO.getPk_psndoc());
        //TODO 查询BD_PSNDOC
        PsndocVO psndocVO = (PsndocVO)getBaseDao().retrieveByPK(PsndocVO.class, userVO.getPk_psndoc());
        yhtUserVO.setUserEmail(psndocVO.getEmail());
        yhtUserVO.setUserMobile(psndocVO.getMobile());
        return yhtUserVO;
    }

    @Override
    public YhtUserVO queryByStaffId(String staffId) throws BusinessException {
        YhtUserVO yhtUserVO = new YhtUserVO();
        //TODO 查询BD_PSNDOC
        PsndocVO psndocVO = (PsndocVO)getBaseDao().retrieveByPK(PsndocVO.class, staffId);
        if(psndocVO == null){
            throw new BusinessException("staff not found");
        }
        yhtUserVO.setUserEmail(psndocVO.getEmail());
        yhtUserVO.setUserMobile(psndocVO.getMobile());
        //TODO 查询SM_USER
        UserVO userVO = (UserVO)getBaseDao().retrieveByClause(UserVO.class, " PK_PSNDOC = '" + staffId + "'").toArray(new UserVO[0])[0];
        if(userVO == null){
            throw new BusinessException("user not found");
        }
        yhtUserVO.setUserCode(userVO.getUser_code());
        yhtUserVO.setUserName(userVO.getUser_name());
        yhtUserVO.setStaffId(userVO.getPk_psndoc());
        yhtUserVO.setUserId(userVO.getPrimaryKey());
        return yhtUserVO;
    }



    @Override
    public YhtFlowVO queryWorkFlow(String flowid) throws BusinessException {
        YhtFlowVO flowVO = new YhtFlowVO();
        flowVO.setFlowId(flowid);
        //TODO 查询SM_APPROVE_MESSAGE SM_USER.CUSERID
        String sqlWhere1 = String.format(" pk_detail = '%s' ", flowid);
        MessageVO messageVO = (MessageVO)getBaseDao().retrieveByClause(MessageVO.class,sqlWhere1).toArray(new MessageVO[0])[0];
        if(messageVO == null) {
            throw new BusinessException("审批流程不存在");
        }
        String billid = messageVO.getBillid();
        String billtype = messageVO.getBilltype();
        String receiver = messageVO.getReceiver();
        String pk_message = messageVO.getPk_message();
        //TODO 查询流单据类型,流程接收人
        YhtUserVO recipient = queryByUserId(receiver);
        flowVO.setSubject(messageVO.getSubject());
        flowVO.setContent(messageVO.getContent());
        flowVO.setBillId(billid);
        flowVO.setBillType(billtype);
        flowVO.setPk_message(pk_message);
        flowVO.setRecipient(recipient);
        //TODO 查询流程流转记录 些操作会物理删除
        WorkflownoteVO workflownoteVO = (WorkflownoteVO)getBaseDao().retrieveByPK(WorkflownoteVO.class, flowid);

        if(workflownoteVO != null){
            flowVO.setBillNo(workflownoteVO.getBillno());
            Integer approvestatus = workflownoteVO.getApprovestatus();
            if(approvestatus == 0){
                //TODO 查询流程实例 某些操作会物理删除
                String sqlWhere2 = "(BILLVERSIONPK = '" + billid + "' or billid = '" + billid + "') and billtype = '" + billtype + "' and workflow_type in (2,4)";
                Collection<FlowInstanceVO> flowVOS = getBaseDao().retrieveByClause(FlowInstanceVO.class, sqlWhere2);
                if(flowVOS != null && flowVOS.size() > 0) {
                    FlowInstanceVO flowDefVO = flowVOS.toArray(new FlowInstanceVO[0])[0];
                    YhtUserVO yhtSender = queryByUserId(flowDefVO.getBillmaker());
                    flowVO.setSubmiter(yhtSender);
                    flowVO.setProcstatus(flowDefVO.getProcstatus());
                    flowVO.setFlowInstId(flowDefVO.getPk_wf_instance());
                }
            }
            flowVO.setFlowType(""+ approvestatus);
            flowVO.setNote(workflownoteVO.getChecknote());
            flowVO.setDealtime(workflownoteVO.getTs().toString());
        }
        return flowVO;
    }
}
